local skynet = require "skynet"
local servicepoolmng = require "incrservicepoolmng"
local configdao = require "configdao"
local tabletool = require "tabletool"
local timetool = require "timetool"
local filelog = require "filelog"
local json = require "cjson"
json.encode_sparse_array(true,1,1)

require "hbslenum"

local server = nil
local tablelist_conf = nil
local nodecmd = {}
local handler = {}

function handler.init_conf()
	tablelist_conf = tabletool.deepcopy(configdao.get_common_conf("tablelist_conf"))
end

function handler.check_leveltype_num(level_type)
	local num = 0
	for table_index, tableitem in pairs(server.public_list) do
		if tableitem.level_type == level_type then num = num + 1 end
	end
	return num
end

function handler.get_level_allplayer_num(level_type)
	local allplayer_num = 0
	for table_index, tableitem in pairs(server.public_list) do
		if tableitem.level_type == level_type then
			allplayer_num = allplayer_num + tableitem.cur_player
		end
	end
	return allplayer_num
end

function handler.check_tablenum_list()
	for level_type, tablelist in pairs(tablelist_conf) do
		local level_num = handler.check_leveltype_num(level_type)
		if level_num == 0 then
			local conf = {
				table_index 	= 0,
				level_type		= level_type,
				table_type  	= HBSLTableType.TABLE_TYPE_ZUANSI,
				max_player_num	= tablelist.max_player_num,
				min_hb_amount	= tablelist.min_hb_amount,
				max_hb_amount	= tablelist.max_hb_amount,
				hb_nums			= tablelist.hb_nums,
				times			= tablelist.times,
				sort_type		= tablelist.sort_type or HBSLBurySortType.BURY_SORT_TYPE_TIME,
				is_closed		= tablelist.is_closed or false,
				play_game_type  = tablelist.play_game_type or 1,
				is_public	  	= 1,
				timeout = 1000,
				name = "root的房间",
	    		create_user_rid = 99999,
	   			create_user_rolename = "system's root",
	    		create_time = timetool.get_time(),
	   			action_qianghb_time = HBSLActionTime.ACTION_TIME_QHB,
				uuid = "",
				cur_player_num = 0,
				curr_round_code = "----",
				service_fee_rate = 0,
			}
			local status, create_table_id, table_index = server:createtable(conf)
		end
	end
end

function nodecmd.init(service)
	if server == nil then server = service end
	handler.init_conf()
end

function nodecmd.start(conf)
	conf.tablesize = conf.tablesize or 10
	conf.tableinrc = conf.tableinrc or 5
	server.idle_table_mng = servicepoolmng:new({}, {service_name="table", service_size=conf.tablesize, incr=conf.tableinrc})
	skynet.fork(function ()
		while true do
			handler.check_tablenum_list()
			skynet.sleep(500)
		end
	end)
end

function nodecmd.get_levellist()
	local levellists = {}
	for level_type, tablelist in pairs(tablelist_conf) do
		local levelitem = {
			level_type 			= level_type,						---场次类型 1初级 2中级 3高级
			play_game_type		= tablelist.play_game_type,			---玩法模式
			min_hb_amount 		= tablelist.min_hb_amount,			---最小红包金额
			max_hb_amount 		= tablelist.max_hb_amount,			---最大红包金额
			cur_total_player 	= 0,								---当前总人数
			hb_num 				= tablelist.hb_nums,			 		---红包个数
			times				= tablelist.times,					---倍数
			is_closed			= tablelist.is_closed or false,		---是否是即将开放
		}
		levelitem.cur_total_player = handler.get_level_allplayer_num(level_type)
		table.insert(levellists, levelitem)
	end
	return levellists
end

function nodecmd.get_levellists()
	--[[
	//匹配场信息
	message matchgame_info {
	optional int32 match_type 	= 1;	//匹配场类型 1初级 2中级 3高级（级别越高底注越高）
	optional int32 dizhu  		= 2;	//底注
	optional int32	ruchang		= 3; 	//入场钻石
	optional int32	lichang		= 4; 	//离场钻石
	optional string conf_intro   = 5;	//设置简介（json编码字符串，这个需要客户端和服务端协商)
	optional int32 max_player   = 6;	//桌子最大人数
	optional int32 cur_total_player = 7;//该匹配场当前总人数
	}
	]]
	local levellists = {}
	for level_type, tablelist in pairs(tablelist_conf) do
		local cp_tablelist = tabletool.deepcopy(tablelist)
		cp_tablelist.game_type = level_type
		local levelitem = {
			match_type 			= level_type,						---场次类型 1初级 2中级 3高级
			dizhu 				= 0,
			ruchang				= 0,
			lichang				= 0,
			conf_intro			= tabletool.deepcopy(cp_tablelist),
			max_player          = cp_tablelist.max_player_num,
			cur_total_player 	= 0,								---当前总人数
		}
		levelitem.cur_total_player = handler.get_level_allplayer_num(level_type)
		table.insert(levellists, levelitem)
	end
	return levellists
end

function nodecmd.get_idle_table_index(level_type)
	local idle_table_index = 0
	for table_index, tableitem in pairs(server.public_list) do
		if tableitem.level_type == level_type and tableitem.cur_player < tableitem.max_player then
			idle_table_index = tableitem.table_index
		end
	end
	return idle_table_index
end

function nodecmd.clear_ridinfo(ispass, rid, table_index, table_type)
	server:unforward(rid, table_index)
end

function nodecmd.get_gamenode_data()
	return server.table_service_map
end

function nodecmd.set_gameend_flag()
	server:set_server_flag(true)
    ---通知robotmgr回写robotinfo
    skynet.send(".robotmgr", "lua", "game_end")
end

function nodecmd.set_tableend_flag(table_code)
	---filelog.sys_error("-----nodecmd--set_tableend_flag-----")
	local server = nodecmd.service
	local table_index = server.code_to_index[table_code]
	if table_index == nil then return false end
	local table_service_obj = server.index_to_table[table_index]
	if table_service_obj == nil then return false end
	local ok, result = pcall(skynet.call, table_service_obj.table_service, "lua", "update_stop_server", true)
end

function nodecmd.delete_table(id, rids)
	server:delete_table(id, rids)
end

return nodecmd